import copy
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from SecureHelperServer.settings import formatResponseData
from utils.conf import Response
from utils.custom import MyJWTAuthentication


class ChangeSettingForAudio(APIView):
    # 局部(本视图有效)认证类
    authentication_classes = [MyJWTAuthentication]
    # 局部(本视图有效)权限类
    permission_classes = [IsAuthenticated]

    def post(self, request, *args, **kwargs):
        """
        设置语音播报
        """
        data = copy.deepcopy(formatResponseData)
        try:
            needAudio = request.data.get("needAudio", None)

            if needAudio is None:
                return Response(data=data, responseMsg="请求参数缺失", loggerMsg="请求参数缺失", isError=True)

            user = request.user
            user.needAudio = needAudio
            user.save()

            return Response(data=data, loggerMsg=f"设置语音播报: 成功，needAudio={needAudio}")

        except Exception as e:
            return Response(data=data, responseMsg="ServerError",
                            loggerMsg=f"请求处理失败: {e}", isError=True)
